package utils 
{
	import adobe.utils.CustomActions;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	/**
	 * ...
	 * @author SChabanov
	 */
	public class CallLater 
	{
		private static var _timer:Timer = new Timer(0,1);
		
		private static var _callStack:Vector.<CallItem>;
				
		public function CallLater() 
		{
			throw new Error("static methods")
		}
		
		/**
		 * 
		 * @param	delay
		 * @param	func
		 * @param	params
		 */
		public static function call(delay:Number, func:Function,params:Object=null):void
		{
			if (!_timer.hasEventListener(TimerEvent.TIMER_COMPLETE))
				_timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerCompleteHandler, false, 0, true);
			
			_timer.delay = delay;			
			if (!_callStack) _callStack = new Vector.<CallItem>();
			_callStack.push(new CallItem(func,params,delay));
			if (!_timer.running)
			{
				_timer.delay = delay;
				_timer.start();
			}
		}
		
		public static function newCall(delay:Number, func:Function,params:Object=null):void
		{
			var citem:CallItem = new CallItem(func, params, delay);
			citem.start();
		}
		
						
		public static function clear():void
		{
			_timer.stop();
			_timer.removeEventListener(TimerEvent.TIMER_COMPLETE, onTimerCompleteHandler);
			_callStack.length = 0;
		}
		
		static private function onTimerCompleteHandler(e:TimerEvent):void 
		{
			if (_callStack.length == 0) 
			{
				_timer.removeEventListener(TimerEvent.TIMER_COMPLETE, onTimerCompleteHandler);
				return;
			}
			var citem:CallItem = _callStack.shift();
			if (citem.params)
			{
				citem.callBack(citem.params);
			}else {
				citem.callBack();
			}
			//_timer.delay = citem.delay;
			_timer.start();
		}
		
		
	}

}
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.TimerEvent;
import flash.utils.Timer;

class CallItem extends EventDispatcher
{
	private var _callBack:Function;
	private var _params:Object;
	private var _delay:Number;
	
	private var _tmr:Timer;
	
	private var _evt:Event = new Event(Event.COMPLETE);
	
	public function CallItem(callBack:Function,params:Object,delay:Number)
	{
		this._delay = delay;
		_callBack = callBack;
		_params = params;
	}
	
	public function start():void
	{
		if (_tmr) return;
		_tmr = new Timer(_delay,1);
		_tmr.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerCompleteHandler);
		_tmr.start();
	}
	
	private function onTimerCompleteHandler(e:TimerEvent):void 
	{
		_tmr.removeEventListener(TimerEvent.TIMER_COMPLETE, onTimerCompleteHandler);
		_callBack(_params);
	}
	
	public function get callBack():Function 
	{
		return _callBack;
	}
	
	public function get params():Object 
	{
		return _params;
	}
	
	public function get delay():Number 
	{
		return _delay;
	}
}